/*************************************************************
 * Title: 
 * Description: 
 * Author: Snow Huang
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com
 * CreateTime: 2010-7-6 下午08:34:54
 * Copyright © 落羽社区 All right reserved
 ************************************************************/
package snow.biz.system.web;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.util.Assert;

import snow.biz.system.dao.UserDao;
import snow.biz.system.model.User;
import snow.biz.system.model.UserFactory;
import snow.core.domain.action.EditAction;
import snow.core.util.algorithm.encoding.PasswordEncoder;
import snow.core.util.constants.ApplicationConstants;
import snow.core.util.toolkit.ServletHelper;

/**
 * 
 */
public class PassportAction extends EditAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1550009900938546219L;

	private String username; // 用户名
	private String password; // 密码
	private String validCode; // 验证码

	private PasswordEncoder passwordEncoder; // 加密工具

	private UserDao userDao; // 用户DAO
	private UserFactory userFactory; // 用户工厂

	/**
	 * @return the password
	 */
	@JSON(serialize = false)
	public String getPassword() {
		return password;
	}

	/**
	 * @return the passwordEncoder
	 */
	@JSON(serialize = false)
	public PasswordEncoder getPasswordEncoder() {
		return passwordEncoder;
	}

	/**
	 * @return the userDao
	 */
	@JSON(serialize = false)
	public UserDao getUserDao() {
		return userDao;
	}

	/**
	 * @return the userFactory
	 */
	@JSON(serialize = false)
	public UserFactory getUserFactory() {
		return userFactory;
	}

	/**
	 * @return the username
	 */
	@JSON(serialize = false)
	public String getUsername() {
		return username;
	}

	/**
	 * @return the validCode
	 */
	@JSON(serialize = false)
	public String getValidCode() {
		return validCode;
	}

	/**
	 * 用户登录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {
		// 1.从持久化设备中查找出User对象
		User user = userDao.findUser(username);
		// 2.判断该对象是否为空,即该用户名是否存在
		Assert.notNull(user, "error.login.user.not.exist");
		// 3.判断用户密码是否匹配
		Assert.isTrue(StringUtils.equalsIgnoreCase(getPasswordEncoder().encodePassword(password), user.getPassword()), "error.login.password.not.match");
		// 4.将用户名写入session中
		getSession().setAttribute(ApplicationConstants.SESSION_USERNAME, username);
		return SUCCESS;
	}

	/**
	 * 退出登录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String logout() throws Exception {
		ServletHelper.clearSession(getSession());
		return SUCCESS;
	}

	/**
	 * 用户注册
	 * 
	 * @return
	 * @throws Exception
	 */
	public String register() throws Exception {
		userFactory.createUser(username, password);
		return SUCCESS;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @param passwordEncoder
	 *            the passwordEncoder to set
	 */
	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}

	/**
	 * @param userDao
	 *            the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	/**
	 * @param userFactory
	 *            the userFactory to set
	 */
	public void setUserFactory(UserFactory userFactory) {
		this.userFactory = userFactory;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @param validCode
	 *            the validCode to set
	 */
	public void setValidCode(String validCode) {
		this.validCode = validCode;
	}

	public void validateLogin() throws Exception {
		validateUsername();
		validatePassword();
		validateValidCode();
	}

	private void validatePassword() throws Exception {
		Assert.hasText(getPassword(), getText("error.login.password.empty"));
	}

	private void validateUsername() throws Exception {
		Assert.hasText(getUsername(), getText("error.login.username.empty"));
	}

	private void validateValidCode() throws Exception {
		Assert.hasText(getValidCode(), getText("error.login.valid.code.empty"));
		String validCode = (String) getSession().getAttribute(ApplicationConstants.SESSION_VALID_CODE);
		Assert.isTrue(StringUtils.equalsIgnoreCase(validCode, getValidCode()), getText("error.login.valid.code.not.match"));
	}

}
